var pool = require("./config/db.js")

function Users(user) {
    if(user){
        this.name=user.name;
        this.surname=user.surname;
        this.phone=user.phone;
        this.country=user.country;
        this.username=user.username;
        this.password=user.password;
        this.email=user.email;
        this.role=user.role;
    }
};

module.exports = Users;

//存储用户信息(er)
Users.prototype.save = function(callback) {
  //要存入数据库的用户文档
  var _obj = {
    name:this.name,
    surname:this.surname,
    phone:this.phone,
    country:this.country,
    username:this.username,
    password:this.password,
    email:this.email,
    role:this.role
  };
    //打开数据库
    pool.getConnection(function(err,connection){
        connection.query('INSERT INTO `users` SET ?',
            _obj,
            function (err, rows, fields) {
                if (err) throw err;
                connection.release();
                callback(null,rows);
            });
    });
};

/**
 * 删一个
 * @param callback
 */
Users.prototype.delete = function(username,callback) {
    var _username= username;
    pool.getConnection(function(err,connection){
        connection.query('DELETE FROM `users` WHERE username=?',
            [_username],
            function (err, rows, fields) {
                if (err) throw err;
                connection.release();
                callback(null,rows);
            });
    });
};

/**
 * 改一个
 * @param callback
 */
Users.prototype.update = function(callback) {
    var _obj = {
        name:this.name,
        surname:this.surname,
        phone:this.phone,
        country:this.country,
        username:this.username,
        password:this.password,
        email:this.email,
        role:this.role
    };
    for(var i in _obj){
        if(!_obj[i]){
            delete _obj[i];
        }
    }
    pool.getConnection(function(err,connection){
        connection.query('UPDATE `users` SET ? WHERE username =?',
            [
                _obj,
                _obj.username
            ],
            function (err, rows, fields) {
                if (err){
                    throw err;
                    callback(err);
                }
                //console.log("sql:::"+this.sql);
                connection.release();
                callback(null,rows);
            });
    });
};

/**
 * 查一个
 * @param callback
 */
Users.prototype.getOne = function(username, callback) {
    var _username= username;
    pool.getConnection(function(err,connection){
        connection.query('SELECT * from `users` WHERE username=?',
            [_username]
            , function (err, rows, fields) {
                if (err) throw err;
                connection.release();
                callback(null,rows);
            });
    });
};

/**
 * 分页查
 * @param pagesize 每页个数
 * @param pages    当前页
 * @param callback
 */
Users.getTen = function(pagesize,pages,callback) {
    var _size = pagesize ? pagesize  : 10,
        _page = pages ? pages : 1,
        _total = null,
        _totalPage=null;

    pool.getConnection(function(err,connection){
        if(err){
            callback("连接池出错");
        }
        if(_total == null){
            connection.query('SELECT COUNT(*) FROM `users`',function(err,rows,fields){
                _total = rows[0]['COUNT(*)'];
                _totalPage = _total / _size;
                _query();
            });
        }
        var _query = function(){
            connection.query('SELECT * from `users` ORDER BY `name` DESC LIMIT ?,?',
                [_totalPage>=1 ? (_page-1)*_size : 0,
                    _totalPage <_page ? (_page*_size) :_size]
                , function (err, rows, fields) {
                    if (err){
                        callback("查询出错");
                    }
                    connection.release();
                    callback(null,rows,_totalPage);
                });
        }
    });
};